﻿Namespace CommandBlocks
    Public Class PairUp
        Public Class Request
            Public BuddyId As Integer
            Public PinCode As String
            Public PassCode As String
            Public Host As String
            Public Port As Integer
            Public Name As String
            Public Email As String
            Public UniqueId As String
        End Class
        Public Class Reply
            Public Success As Boolean
            Public PassCode As String
            Public Name As String
            Public Email As String
            Public UniqueId As String
        End Class

        Friend Shared Function HandleRequest(ByVal node As Node, ByVal commandBlock As MessageBlock) As Boolean
            Dim clsArgs As Request = Framework.Serialization.FromXmlBytes(Of Request)(commandBlock.Message)

            Dim clsBud As Entities.Buddy = Framework.Buddies.Manager.GetById(clsArgs.BuddyId)
            If clsBud Is Nothing Then
                Manager.OnNodeLogEvent(node, String.Format("Buddy #{0} does not exist", clsArgs.BuddyId))
                Return False
            End If
            If Not clsBud.PairUpPinCode = clsArgs.PinCode Then
                Manager.OnNodeLogEvent(node, String.Format("Buddy #{0} pin code does not match", clsArgs.BuddyId))
                Return False
            End If
            If Not clsBud.PairUpValidUntil > Now.ToUniversalTime Then
                Manager.OnNodeLogEvent(node, String.Format("Buddy #{0} pin code has expired", clsArgs.BuddyId))
                Return False
            End If
            clsBud.PairUpPinCode = Nothing
            clsBud.PairUpValidUntil = DateTime.MinValue
            clsBud.LastConnected = Now.ToUniversalTime
            clsBud.UniqueId = clsArgs.UniqueId
            clsBud.Name = clsArgs.Name
            clsBud.NameLocal = clsArgs.Name
            clsBud.Host = clsArgs.Host
            clsBud.Port = clsArgs.Port
            clsBud.Email = clsArgs.Email
            clsBud.PassCode_LoginAtBuddy = clsArgs.PassCode
            clsBud.PassCode_LoginHere = Framework.Misc.GeneratePassCode(64)

            node.Buddy = clsBud

            Framework.Buddies.Manager.Save(clsBud)

            Dim clsReply As New Reply
            clsReply.Success = True
            clsReply.Email = Framework.Net.Settings.Email
            clsReply.Name = Framework.Net.Settings.Nickname
            clsReply.PassCode = clsBud.PassCode_LoginHere
            clsReply.UniqueId = Framework.Net.Settings.UniqueId

            node.PostCommandMessage("PAIRUPREPLY", Framework.Serialization.ToXmlBytes(clsReply))
            Return True
        End Function

        Friend Shared Function HandleReply(ByVal node As Node, ByVal pairUpInfo As PairUpInfo, ByVal request As Request, ByVal commandBlock As MessageBlock) As Boolean
            Dim clsArgs As Reply = Framework.Serialization.FromXmlBytes(Of Reply)(commandBlock.Message)

            If Not clsArgs.Success Then
                Manager.OnNodeLogEvent(node, "Pair up failed")
                Return False
            End If

            Dim clsBud As New Entities.Buddy
            clsBud.Email = clsArgs.Email
            clsBud.Host = pairUpInfo.Host
            clsBud.LastConnected = Now.ToUniversalTime
            clsBud.Name = clsArgs.Name
            clsBud.NameLocal = clsArgs.Name
            clsBud.PassCode_LoginAtBuddy = clsArgs.PassCode
            clsBud.PassCode_LoginHere = request.PassCode
            clsBud.Port = pairUpInfo.Port
            clsBud.UniqueId = clsArgs.UniqueId

            node.Buddy = clsBud

            Framework.Buddies.Manager.Save(clsBud)
            Return True
        End Function

    End Class



End Namespace